package cn.player.act;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import cn.player.R;
import cn.player.adapter.LinearLayoutColorDivider;
import cn.player.adapter.RecycleViewItemAdapter;
import cn.player.entity.TestBean;

/**
 * Created by zengliang on 16/11/28.
 */

public class DiffUtilsTestActivity extends AppCompatActivity {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private RecycleViewItemAdapter mRecycleViewItemAdapter;
    private List<TestBean> mCurrentData;


    private static final int COUNT = 4;
    private int mCount = COUNT;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diff_utils_test);

        findViews();
        initData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_refresh, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_refresh:
                refreshData();
                break;
            case R.id.menu_add_item:
                mRecycleViewItemAdapter.notifyInserted(1, new TestBean("新加的条目标题111", "新加的条目简介111"));
                break;
            case R.id.menu_remove_item:
                mRecycleViewItemAdapter.notifyRemoved(2);
                break;
            case R.id.menu_update_item:
                mRecycleViewItemAdapter.notifyChanged(3,new TestBean("更新的条目标题333", "更新的条目简介333"));
                break;
        }
        return true;
    }

    private void refreshData() {
        List<TestBean> newData = new ArrayList<>();
        for (int i = 2; i < 50; i++) {
            if (i % mCount == 0) {
                newData.add(new TestBean("被修改的标题" + i, "被修改的简介" + i));
            } else {
                newData.add(new TestBean("测试数据标题" + i, "测试简介" + i));
            }
        }

        mCount--;
        if (mCount == 0) {
            mCount = COUNT;
        }

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallback(mCurrentData, newData));
        diffResult.dispatchUpdatesTo(mRecycleViewItemAdapter);

        mCurrentData = newData;
        mRecycleViewItemAdapter.setupRecycleViewData(mCurrentData);
    }

    private void findViews() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_layout);
        mRecyclerView = (RecyclerView) findViewById(R.id.rcv_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(), R.color.green, R.dimen.divider_height_thin, LinearLayoutManager.VERTICAL));

        mRecycleViewItemAdapter = new RecycleViewItemAdapter();
        mRecyclerView.setAdapter(mRecycleViewItemAdapter);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRecycleViewItemAdapter.notifyInserted(0, new TestBean("新加的条目标题000", "新加的条目简介000"));
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    private void initData() {

        mCurrentData = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            mCurrentData.add(new TestBean("测试数据标题" + i, "测试简介" + i));
        }

        mRecycleViewItemAdapter.setupRecycleViewData(mCurrentData);
        mRecycleViewItemAdapter.notifyDataSetChanged();

    }

    class DiffCallback extends DiffUtil.Callback {

        private List<TestBean> oldData, newData;

        public DiffCallback(List<TestBean> oldData, List<TestBean> newData) {
            this.oldData = oldData;
            this.newData = newData;
        }

        @Override
        public int getOldListSize() {
            return oldData != null ? oldData.size() : 0;
        }

        @Override
        public int getNewListSize() {
            return newData != null ? newData.size() : 0;
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldData.get(oldItemPosition).getName().equals(newData.get(newItemPosition).getName());
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return oldData.get(oldItemPosition).getDescription().equals(newData.get(newItemPosition).getDescription());
        }

        @Nullable
        @Override
        public Object getChangePayload(int oldItemPosition, int newItemPosition) {
            return super.getChangePayload(oldItemPosition, newItemPosition);
        }
    }

}
